#!/bin/bash

set -e
source "${SCRIPTS_DIR}"/lib/debug_functions

# The generators assume GOPATH is set
export GOPATH
GOPATH="$(go env GOPATH)"

K8S_IO_DIR="${GOPATH:-$HOME/go}/src/k8s.io"
CODEGEN_SCRIPT_DIR="${K8S_IO_DIR}/code-generator"
CODEGEN_SCRIPT="${CODEGEN_SCRIPT_DIR}/generate-groups.sh"
GENGO_BOILERPLATE_DIR="${K8S_IO_DIR}/gengo"
GENGO_BOILERPLATE="${GENGO_BOILERPLATE_DIR}/boilerplate/boilerplate.go.txt"
CODEGEN_RELEASE_TAG=kubernetes-1.29.1

if [ ! -f "$CODEGEN_SCRIPT" ]; then
    echo "$CODEGEN_SCRIPT does not exist - downloading..."
    (
        git clone --branch $CODEGEN_RELEASE_TAG https://github.com/kubernetes/code-generator "${CODEGEN_SCRIPT_DIR}"
        cd "${CODEGEN_SCRIPT_DIR}"
        go mod vendor
        echo "Successfully checked out release tag $CODEGEN_RELEASE_TAG"
    )
fi

if [ ! -f "$GENGO_BOILERPLATE" ]; then
    echo "$GENGO_BOILERPLATE does not exist - downloading..."
    git clone https://github.com/kubernetes/gengo "${GENGO_BOILERPLATE_DIR}"
fi

"${CODEGEN_SCRIPT}" applyconfiguration,client,deepcopy,informer,lister github.com/submariner-io/submariner/pkg/client github.com/submariner-io/submariner/pkg/apis submariner.io:v1
